<?php
namespace Home\Controller;
use Home\Controller\PublicController;
class GoodsController extends PublicController
{
	//商品列表
	public function index()
	{
		//---导航信息
		$this->navigation1();
		//---导航信息
		//一、遍历类别
		$where['pid']=1;//定义查询条件
		$type_arr=M('Type')->where($where)->field('id,type_name')->select();//查询think_type表，得到类别
		$this->assign('type_arr',$type_arr);//赋值类别信息

		// 获取过滤条件
		$type_id=I('get.type_id');
		$ct=I('get.ct');
		$material=I('get.material');
		$price=I('get.price');
		
		//遍历商品
		//定义搜索条件
		$map=array();
		if(!$_GET){
			session('type_id',null);
			session('ct',null);
			session('ctFilter',null);
			session('material',null);
			session('price',null);
			session('priceFilter',null);
			session('txtkey',null);
		}

		if($type_id){
			//获取到type_id，添加map条件，并写入session
			$map['type_id']=$type_id;
			session('type_id',$type_id);
		}elseif(session('type_id')){
			$map['type_id']=session('type_id');
		}

		if($ct){
			// 将接收到的ct写入session
			session('ct',$ct);
			// 将得到的ct转换成所需格式
			$ctFilter=explode('-',$ct);
			foreach($ctFilter as $k=>$v){$ctFilter[$k]=$v/100;}

			// 将ctFilter添加到map条件，并写入session
			$map['ct']=array('between',$ctFilter);
			session('ctFilter',$map['ct']);

		}elseif(session('ctFilter')){
			$map['ct']=session('ctFilter');
		}

		if($material){
			// 添加map条件，并写入session
			$map['material']=$material;
			session('material',$map['material']);

		}elseif(session('material')){
			$map['material']=session('material');
		}

		if($price){
			// 将接受到的price写入session
			session('price',$price);

			if($price==40000){
				// 若price为40000，则map条件设为大于40000
				$map['price']=array('gt','40000');
				session('priceFilter',$map['price']);
			}else{
				$priceFilter=explode('-',$price);
				$map['price']=array('between',$priceFilter);
				session('priceFilter',$map['price']);
			}
		}elseif(session('priceFilter')){
			$map['price']=session('priceFilter');
		}
		// 搜索
		if(I('post.txtkey')){
			$txtkey=I('post.txtkey');
			$this->assign('txtkey',$txtkey);
			$map['style']=array('like',"%{$txtkey}%");
		}

		//分页
		//查询满足要求的总记录数
		$count=M('Goods')
			->where($map)
			->join('RIGHT JOIN think_goods_detail as detail ON detail.goods_id=think_goods.id')
			->count();
		$num=12;//每页显示数量
		$pages=ceil($count/$num);//总页数
		$page=new \Think\Page($count,$num);//实例化分页类
		$show=$page->show();
		//进行分页查询
		$goodsDetail=M('Goods')
			->where($map)
			->order('goods_id desc')
			->limit($page->firstRow.','.$page->listRows)
			->join('RIGHT JOIN think_goods_detail as detail ON detail.goods_id=think_goods.id')
			->select();
		$this->assign('count',$count);
		$this->assign('pages',$pages);
		$this->assign('page',$show);
		//foreach给goods_arr添加type_name字段
		$type=M('Type');
		foreach ($goodsDetail as $k=>$v){
			$goodsDetail[$k]['type_name']=$type->where(array('id'=>$v['type_id']))->getfield('type_name');
			$goodsDetail[$k]['ct2']=$goodsDetail[$k]['ct']*100;//卡拉转为分
			$goodsDetail[$k]['type_name2']=strtoupper($goodsDetail[$k]['type_name']);//系列转为大写
			$goodsDetail[$k]['pid']=$type->where(array('id'=>$v['type_id']))->getfield('pid');
			$goodsDetail[$k]['comment']=M('news')->where(array('goods_id'=>$v['goods_id']))->count();
		}
		$this->assign('goodsDetail',$goodsDetail);//赋值商品信息
		$this->display('list');//输出模板
	}
	

	public function filter(){
		$get=I('get.remove');
		$getFilter=$get."Filter";
		session($get,null);
		session($getFilter,null);
		$this->index();
	}
	/*public function price1(){//显示商品价格
		$goods_id=I('post.goods_id');//ajax得到goods_id
		$goods_detail_id=I('post.goods_detail_id');//goods_detail_id
		$ct=I('post.ct');//ajax得到ct
		$color=I('post.color');//ajax得到color

		$m=M('goods_detail');
		$where=array('goods_id'=>$goods_id,'ct'=>$ct,'color'=>$color);
		$price=$m->where($where)->getfield('price');
		$id=$m->where($where)->getfield('id');
		//$this->assign('price',$price);
		$data['id']=$id;
		$data['goods_detail_id']=$goods_detail_id;
		$data['price']=$price;
		$this->ajaxReturn($data);
	}*/


	public function details(){
		//---导航信息
		$this->navigation1();
		//---导航信息
		$data['id']=I('get.id');//从列表页get获取到商品的goods_detail_id
		// $this->assign('goods_detail_id',$data['id']);
		######################################
		$m=M('goods_detail');
		/*if($data['id']){//判断选择商品条件后传来的id冰进行查询
			//查询id为$data['id']下的。。。
			$result=$m->field('goods_id,ct,color,clarity,cut,material,price')->find($data['id']);
		}*/
		$result=$m->field('goods_id,ct,color,clarity,cut,material,price')->find($data['id']);
		$goods_id=$result['goods_id'];//商品的goods_id
		$goods_detail_arr=$m->where('goods_id='.$goods_id)->select();//查询good_id下所有规格详情
		// dump($goods_detail_arr);
		#######################################
		$p=M('goods');//查询goods表
		//style 款 goods_name 名字
		$pic=$p->field('sales,type_id,goods_name,style,pic1,pic2,pic3,pic4')->find($goods_id);
		$typeid['id']=$pic['type_id'];
		$name=M('type');
		$type_name=$name->where($typeid)->field('type_name')->find();//系列

		#######################################
		#分页#
		$note=M('news');
		$where2=array('goods_id'=>$data['id']);
		$count=$note->where($where2)->count();
		$page=new\Think\Page($count,4);
		//留言查询的条件
		// $news=$note->where($where2)->field('users_id,content,addtime')->select();//查询留言的用户id，留言内容，留言时间
		// 
		$news=$note->limit($page->firstRow.','.$page->listRows)->where($where2)->join('think_users ON think_news.users_id = think_users.id')->select();
		$num=$note->where($where2)->count();//留言数量

			 foreach($news as $k=>$v){
				$news[$k]['tel']=substr_replace($v['tel'],'****',3,5);//把替换过的电话号码装回$news['tel']中
			}

		
		
		$this->assign('news',$news);//将结果抛给index.html
		//分页按钮设置
		$page->rowPage=1;
		$page->lastSuffix=false;
		$page->setConfig('header', '<li class="rows">第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');
        $page->setConfig('last', '末页');
        $page->setConfig('first', '首页');
        $page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $page->lastSuffix = false;//最后一页不显示为总页数
        $show=$page->show();
        //dump($page);
		$this->assign('page',$show);
	
		#######################################
		$this->assign('num',$num);//评论数量
		$this->assign('news',$news);//评论内容
		$this->assign('goods_id',$goods_id);
		$this->assign('type_name',$type_name);//类型名字
		$this->assign('goods_detail_arr',$goods_detail_arr);//规格详情
		$this->assign('pic',$pic);//图片
		$this->assign('result',$result);//商品的详细信息
		$this->display();
	}


	public function addCart(){
		$data['uid']=session('user_id');//获取用户id
		if(!$data['uid']){
			// $this->error('请登录',U('Index/login'));
			$da['status']=2;
			$da['msg']="请登录！";
			$this->ajaxReturn($da);
		}

		$goods_id=I('post.goods_id');//ajax获取goods_id
		$data['goods_detail_id']=I("post.goods_detail_id");//ajax获取goods_detail_id
		$data['price']=I('post.price');//ajax获取价格

		$data['material']=M('Goods_detail')->where(array('id'=>$data['goods_detail_id']))->getfield('material');//获取material
		$goods_arr=M('Goods')->field('goods_name,style')->find($goods_id);

		$data['goods_name']=$goods_arr['goods_name'];//获取goods_name
		$data['goods_style']=$goods_arr['style'];//获取goods_style
		$data['num']=1;
		
		$res=M('Cart')->add($data);
		if($res){
			$da['status']=1;
			$da['msg']="添加成功！";
			$this->ajaxReturn($da);
			//$this->success('添加成功');
		}else{
			$da['status']=0;
			$da['msg']="添加失败！";
			$this->ajaxReturn($da);
			// $this->error('添加失败');
		}

	}
	public function buy(){
		$data['uid']=session('user_id');//获取用户id
		if(!$data['uid']){
			// $this->error('请登录',U('Index/login'));
			$da['status']=2;
			$da['msg']="请登录！";
			$this->ajaxReturn($da);
		}

		$goods_id=I('post.goods_id');//ajax获取goods_id
		$data['goods_detail_id']=I("post.goods_detail_id");//ajax获取goods_detail_id
		$data['price']=I('post.price');//ajax获取价格

		$data['material']=M('Goods_detail')->where(array('id'=>$data['goods_detail_id']))->getfield('material');//获取material
		$goods_arr=M('Goods')->field('goods_name,style')->find($goods_id);

		$data['goods_name']=$goods_arr['goods_name'];//获取goods_name
		$data['goods_style']=$goods_arr['style'];//获取goods_style
		$data['num']=1;
		
		$res=M('Cart')->add($data);
		if($res){
			$da['status']=1;
			$this->ajaxReturn($da);
			//$this->success('添加成功');
		}else{
			$da['status']=0;
			$da['msg']="添加失败！";
			$this->ajaxReturn($da);
			// $this->error('添加失败');
		}

	}

	/*public function buy(){
		$data['uid']=session('user_id');//获取用户id
		$data['price']=I('get.price');
		if(!$data['uid']){
			$this->error('请登录',U('Index/login'));
		}
		if(!$data['price']){
			$this->error('请选择规格');
		}
			$goods_detail_id=I("get.id");//获取goods_detail_id
			$goods_detail_arr=M('Goods_detail')->field('goods_id,material')->find();//获取goods_id
			$goods_arr=M('Goods')->field('goods_name,style')->find($goods_id);
			$data['goods_name']=$goods_arr['goods_name'];
			$data['goods_style']=$goods_arr['style'];
			$data['material']=$goods_detail_arr['material'];
			$data['num']=1;
			$res=M('Cart')->add($data);
			if($res){
				redirect(U('User/cart'));
			}
	}*/

}

